package com.example.admin.brainwave.net;

import android.app.Activity;
import android.text.TextUtils;

import com.example.admin.brainwave.bean.HistoryBean;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.ITaskCallbackListener;
import com.example.admin.brainwave.utils.JsonUtils;
import com.example.admin.brainwave.utils.OkHttpUtil;
import com.example.admin.brainwave.utils.ProgressDialogManager;
import com.example.admin.brainwave.utils.RxTask;
import com.example.admin.brainwave.utils.SPHelper;

import java.util.HashMap;


public class HistoryTask extends RxTask<String, Integer, HistoryBean> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public HistoryTask(Activity activity,
                       ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected HistoryBean doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.post(Const.TEST_RECORD, getParam());
        if (!TextUtils.isEmpty(result)) {
            HistoryBean bean = JsonUtils.parseObject(result, HistoryBean.class);
            return bean;
        }
        return null;
    }

    @Override
    protected void onPostExecute(HistoryBean result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", SPHelper.getInstance(activity).getString("userId", ""));
        return params;
    }
}